import axios from 'axios'
import { Toast } from 'vant'
import qs from 'qs'
axios.defaults.baseURL = '/api'
// axios.defaults.headers = {
//   Authorization: window.sessionStorage.getItem('zxgToken'),
// }
axios.interceptors.request.use((config) => {
  config.headers.Authorization = window.sessionStorage.getItem('zxgToken')
  return config
})
// 登录请求
const login = async (params) => {
  return await axios
    .post('/user/login', params)
    .then((res) => {
      // console.log('用户', res)
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
const getUserQQinfo = async (params, cb) => {
  return await axios
    .get('/user/qqinfo', {
      params,
    })
    .then((res) => {
      cb && cb(res.data)
    })
    .catch((err) => {
      console.log(err)
    })
}
const getUserQQlogin = async (params, cb) => {
  return await axios
    .post('/user/qqlogin', qs.stringify(params))
    .then((res) => {
      cb && cb(res.data)
    })
    .catch((err) => {
      console.log(err)
    })
}
// 获取用户数据
const getUserInfo = async (cb) => {
  return await axios
    .get('/user/info')
    .then((res) => {
      cb && cb(res.data)
    })
    .catch((err) => console.log(err))
}
// 获取首页分类导航数据
const getCategoryNav = async () => {
  return await axios
    .get('/home/category')
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}

// 获取首页轮播图

const getSlideHome = async () => {
  return await axios
    .get('/home/sliders')
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 数据分类数据
const getCategory = async () => {
  return await axios
    .get('/category/data')
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 获取楼层数据
const getFloorCon = async () => {
  return await axios
    .get('/home/floorlist')
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 获取商品列表
const getProductList = async (params) => {
  return await axios
    .get('/product/list', params)
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 获取详情页数据
const getDetailInfo = async (params) => {
  return await axios.get('/product/detail', params).then((res) => {
    const { data } = res
    if (data.status == 0) {
      return data
    } else {
      Toast.fail(data.msg)
    }
  })
}
// 单选
const addSingleShopCar = async (params) => {
  return await axios
    .post('cart/add', params)
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 获取购物车数据
const getCarList = async () => {
  return await axios
    .get('/cart/list')
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 删除商品
const delMyGoods = async (params) => {
  return await axios
    .post('/cart/del', qs.stringify(params))
    .then((res) => {
      const { data } = res
      if (data.status == 0) {
        // return data
        Toast(data.msg)
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 获取推荐数据
const getRecommend = async () => {
  return await axios
    .get('/product/recommend')
    .then((res) => {
      const { data } = res
      // console.log(data)
      if (data.status == 0) {
        return data
      } else {
        Toast.fail(data.msg)
      }
    })
    .catch((err) => console.log(err))
}
// 退出登录
const logOut = async () => {
  return await axios
    .post('/user/logout')
    .then((res) => {
      if (res.status == 0) {
        window.sessionStorage.clear('zxgToken')
      } else {
        Toast.fail(res.data.msg)
      }
    })
    .catch((err) => console.log(err))
}
export {
  login,
  getUserQQinfo,
  getUserQQlogin,
  getSlideHome,
  getCategoryNav,
  getFloorCon,
  getProductList,
  getUserInfo,
  getDetailInfo,
  addSingleShopCar,
  getCarList,
  delMyGoods,
  getRecommend,
  getCategory,
  logOut,
}
